#include "stdafx.h"
#include "Player.h"
#include "HandInfo.h"
#include <cassert>
#include <iostream>

HandInfo::HandInfo(const Hand &h)
{
	hand_ = h;
	const list<Player> &playerList = h.Players();
	list<Player>::const_iterator playerIt;

	if (playerList.empty()) assert(0); // Error check: ensure player list is non empty
	
	PlayerHandInfo *infoPrevPtr = NULL;
	for (playerIt = playerList.begin(); playerIt != playerList.end(); playerIt++)
	{
		PlayerHandInfo *infoCurr = new PlayerHandInfo;
		infoCurr->MyPlayer(*playerIt);

		if (infoPrevPtr != NULL)
		{
			infoPrevPtr->Left(infoCurr);
			infoCurr->Right(infoPrevPtr);
		}

		//handInfoPtr->Add(playerIt->Name(), infoCurr);
		playerInfoMap_[playerIt->Name()] = infoCurr;
		infoPrevPtr = infoCurr;
	}
	
	playerIt = playerList.begin();

	string firstPlayerName = playerIt->Name();
	playerIt = playerList.end();
	playerIt--;
	string lastPlayerName = playerIt->Name();

	PlayerHandInfo &firstPlayerHandInfo = this->PlayerInfo(firstPlayerName);
	PlayerHandInfo &lastPlayerHandInfo = this->PlayerInfo(lastPlayerName);

	firstPlayerHandInfo.Right(&lastPlayerHandInfo);
	lastPlayerHandInfo.Left(&firstPlayerHandInfo);

	/*map<string,PlayerHandInfo *> playerInfoMap = handInfoPtr->PlayerInfoMap();
	map<string,PlayerHandInfo *>::iterator it;*/

	/*for (it = playerInfoMap.begin(); it !=playerInfoMap.end(); it++)
	{
		cout << (*it).first << "\tLeft: " << (*it).second->Left()->PlayerName()
			<< "\tRight: " << (*it).second->Right()->PlayerName() << endl;
	}*/

	//return handInfoPtr;
}

PlayerHandInfo HandInfo::PlayerInfoBySeat(int num)
{
	list<Player>::iterator playerListIter;
	string playerName;
	for (playerListIter = hand_.Players().begin(); playerListIter != hand_.Players().end(); playerListIter++)
	{
		if (playerListIter->SeatNum() == num)
		{
			playerName = playerListIter->Name();
			break;
		}
	}

	if (playerName.empty())
	{
		throw "No player sitting at seat";
	}

	return *(playerInfoMap_[playerName]);
}